﻿@model PostViewModel

@{ var currentUrl = Url.AbsoluteAction("PostDetails", new
														  {
															  Model.Post.Id,
															  Model.Post.Slug
														  }); }

@{
	ViewBag.Title = @Model.Post.Title;
	ViewBag.CurrentUrl = currentUrl;
}

<article class="post">
	<header>
		<h2>
			@Html.RouteLink(Model.Post.Title, "PostDetails", new
																 {
																	 Model.Post.Id,
																	 Model.Post.Slug
																 })
			<small>@Model.Post.Author.Name - @Model.Post.PublishedAt.ToReadableOffset(DateTime.UtcNow)</small>
		</h2>
	</header>

	<nav>
		<ul class="pager">
			@if (Model.PreviousPost != null)
			{
				<li class="previous">
					<a href="@Url.RouteUrl("PostDetails", new
															  {
																  Model.PreviousPost.Id,
																  Model.PreviousPost.Slug
															  })" title="@Model.PreviousPost.Title">
						@Html.Substring(Model.PreviousPost.Title, 25)
					</a>
				</li>
			}

			@if (Model.NextPost != null)
			{
				<li class="next">
					<a href="@Url.RouteUrl("PostDetails", new
															  {
																  Model.NextPost.Id,
																  Model.NextPost.Slug
															  })">
						@Html.Substring(Model.NextPost.Title, 25)
					</a>
				</li>
			}
		</ul>
	</nav>

	<div class="post-body">
		@Html.Raw(Model.Post.Body)
	</div>

	@if (Model.Post.Tags.Any())
	{
		<footer class="well">
			<p class="tags">
				Tags
				@foreach (var tag in Model.Post.Tags)
				{
					<span class="label">@Html.RouteLink(tag.Name, "PostsByTag", new
																					{
																						tag.Slug
																					})</span>
				}
			</p>
		</footer>
	}

	<div class="share">
		<div class="twitter-share">
			@Html.TwitterButton("Tweet", TwitterButtonDataCount.Horizental, Model.Post.Author.Name, (string)ViewBag.CurrentUrl, Model.Post.Title)
		</div>
	</div>

	<div class="feed">
		<a rel="alternate" type="application/rss+xml" href="@Url.Action("CommentsRss", "Syndication", new
																										  {
																											  Model.Post.Id
																										  })">Comments Feed</a>
	</div>

	<hr />
    
	<section class="comments">
		<header>
			<h3>What others have said...</h3>
		</header>

		@if (!Model.Post.Comments.Any())
		{
			<article class="no-comments">
				No comments posted yet.
			</article>
		}

		@{ var i = 0; }

		@foreach (var comment in Model.Post.Comments.OrderByDescending(c => c.CreatedAt))
		{
			<article id="comment-@(comment.Id)" class="comment">
				<div class="avatar">
					@Html.GravatarImage(comment.EmailAddress, GravatarDefaultImage.Identicon, new
																								  {
																									  alt = comment.Author
																								  })
				</div>

				<p class="comment-body">
					@Html.Raw(comment.Body)
				</p>

				<address>
					@Html.Link(comment.Author, comment.Url, new
																{
																	target = "_blank"
																}) - @comment.CreatedAt.ToReadableOffset(DateTime.UtcNow)
				</address>
			</article>

			if (++i < Model.Post.Comments.Count)
			{
				<hr />
			}
		}

		<article class="add-comment">
			@if (!Model.Post.AllowComments || Model.Post.AreCommentsClosed)
			{
				<p>Comments have been closed.</p>
			}
			else
			{
				@Html.Alert(Html.ValidationSummary(), AlertType.Error)

				using (Html.BeginForm(null, FormMethod.Post, new
																 {
																	 @class = "form-horizontal"
																 }))
				{
					@Html.AntiForgeryToken()

					<fieldset>
						<legend>What do you have to say?</legend>

						@Html.EditorGroupFor(x => x.Comment.Author, "*")
						@Html.EditorGroupFor(x => x.Comment.EmailAddress, "*")
						@Html.EditorGroupFor(x => x.Comment.Url, "*")
						@Html.EditorGroupFor(x => x.Comment.Body, "*")

						@if (!Model.IsTrustedCommenter)
						{
							using (Html.ControlGroup("recaptcha_response_field"))
							{
								@Html.Label("recaptcha_response_field", "Are you human?", new
																							  {
																								  @class = "control-label"
																							  })
								<div class="controls">
									@Html.Captcha(CaptchaTheme.White)
									@Html.ValidationMessage("recaptcha_response_field", "*", new
																								 {
																									 @class = "help-inline"
																								 })
								</div>
							}
						}

						<div class="form-actions">
							@Html.SubmitButton("Post Comment", new
																   {
																	   @class = "btn btn-primary"
																   })
							@Html.ResetButton("Cancel", new
															{
																@class = "btn"
															})
						</div>
					</fieldset>
				}
			}
		</article>
	</section>
</article>

@section Scripts
{
	@if (Request.IsSecureConnection)
	{
		<script type="text/javascript" src="https://platform.twitter.com/widgets.js"></script>
		<script type="text/javascript" src="https://static.ak.fbcdn.net/connect.php/js/FB.Share"></script>
	}
	else
	{
		<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
		<script type="text/javascript" src="http://static.ak.fbcdn.net/connect.php/js/FB.Share"></script>
	}
}